/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cafe.Controller;

import cafe.Model.Utility.LoadServerConfig;
import hapax.TemplateException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.log.Log;

public class ControllerInterface extends HttpServlet {
    
    List<String> config = LoadServerConfig.loadConfigServer();
    
    String temp = "http://" + config.get(0).toString() + ":" + config.get(1).toString();
    
    public String STATICHOST = temp;
    
//    public static final String STATICHOST

    public TemplateCollection templateCollection = TemplateCollection.getInstance();
    private static final org.eclipse.jetty.util.log.Logger LOG = Log.getLogger(ControllerInterface.class);

    public String index(HttpServletRequest request, HttpServletResponse response, String context, String URL) {
        try {
            //""
            context = templateCollection.getTemplate(URL).renderToString(templateCollection.getTemplateDataDictionary());
        } catch (TemplateException ex) {
            Logger.getLogger(IndexServletController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return context;
    }

    public String listItem(HttpServletRequest request, HttpServletResponse response, String context, String URL) {
        try {
            //""
            context = templateCollection.getTemplate(URL).renderToString(templateCollection.getTemplateDataDictionary());
        } catch (TemplateException ex) {
            Logger.getLogger(IndexServletController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return context;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = "";
        String str = "";
        
        if(request.getParameter("do") != null) {
            action = request.getParameter("do");
        } else {
            action = "index";
        }
        
        str = doAction(action, request, response, str);

//        LOG.info(action);

        request.setCharacterEncoding("UTF-8");
//        request.setContentType("text/html; charset=UTF-8;");
        response.setContentType("text/html; charset=UTF-8;");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println(str);

    }

    public String getItem(HttpServletRequest request, HttpServletResponse response, String context) {
        return context;
    }

    public String delete(HttpServletRequest request, HttpServletResponse response, String context) {
        return context;
    }

    public String edit(HttpServletRequest request, HttpServletResponse response, String context) {
        return context;
    }

    public String addItem(HttpServletRequest request, HttpServletResponse response, String context) {
       
        return context;
    }

    public String doAction(String action, HttpServletRequest request, HttpServletResponse response, String context) {
        return "";
    }
}
